package com.ratta.supernote.plugin.sticker.lib.view.dashView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

public class DashView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    private float dashWidth;   // 虚线线段宽度
    private float gapWidth;    // 空白间隔宽度
    private int lineColor;     // 线条颜色
    private boolean isVertical; // 是否垂直方向

    public DashView(Context context) {
        super(context);
        init();
    }

    public DashView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        
        // 默认值
        dashWidth = 2f;
        gapWidth = 5f;
        lineColor = 0xFF000000;
        isVertical = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        paint.setColor(lineColor);
        // 添加dp转px逻辑
        paint.setStrokeWidth(1);
        
        // 创建虚线效果
        float[] intervals = {2, 5};
        paint.setPathEffect(new DashPathEffect(intervals, 0));

        if (isVertical) {
            // 垂直虚线
            path.moveTo(getWidth()/2, 0);
            path.lineTo(getWidth()/2, getHeight());
        } else {
            // 水平虚线
            path.moveTo(0, getHeight()/2);
            path.lineTo(getWidth(), getHeight()/2);
        }
        
        canvas.drawPath(path, paint);
        path.reset();
    }

    // 设置方向 (水平/垂直)
    public void setVertical(boolean vertical) {
        isVertical = vertical;
        requestLayout();
        invalidate();
    }

    // 设置虚线参数
    public void setDashWidth(float dashWidth) {
       
        this.dashWidth = dashWidth;
        invalidate();
    }

    public void setGapWidth(float gapWidth) {
        
        this.gapWidth = gapWidth;
        invalidate();
    }

    // 设置线条颜色
    public void setLineColor(int color) {
        this.lineColor = color;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = isVertical ? (int) paint.getStrokeWidth() : MeasureSpec.getSize(widthMeasureSpec);
        int height = isVertical ? MeasureSpec.getSize(heightMeasureSpec) : (int) paint.getStrokeWidth();
        setMeasuredDimension(width, height);
    }
}